import os
import relic
from PIL import Image, ImageDraw, ImageFont
from relic_train_img_generator import generate_relic_img
import random
from utils import text_util

font_18 = ImageFont.truetype("../font.ttf", size=18)
font_20 = ImageFont.truetype("../font.ttf", size=20)
font_28 = ImageFont.truetype("../font.ttf", size=28)
font_35 = ImageFont.truetype("../font.ttf", size=35)

relic_infos = []
images = []
# [["乐园遗落之花","时之沙","凝结的时刻","元素精通提高80点。"
# ,"装备者绽放、超绽放、烈绽放反应造成的伤害提升40%。此外，装备者触发绽放、超绽放、烈绽放后，上述效果带来的加成提升25%，
# 该效果持续10秒，至多叠加4次，每1秒至多触发一次。装备者处于队伍后台时依然能触发该效果。"]]
src = "images/圣遗物"
for coordinate_name in os.listdir(src):
    两件套 = "2件套："
    四件套 = "4件套："
    for s in open(src + "/" + coordinate_name + "/两件套.txt", encoding="utf-8").readlines():
        两件套 += s

    for s in open(src + "/" + coordinate_name + "/四件套.txt", encoding="utf-8").readlines():
        四件套 += s

    两件套 = text_util.auto_next_line(s=两件套, font=font_20, length=308)
    四件套 = text_util.auto_next_line(s=四件套, font=font_20, length=308)
    for z in os.listdir(src + "/" + coordinate_name):
        if z.endswith("png"):
            relic_info = [coordinate_name, z[0:3], z[4:].replace(".png", ""), 两件套, 四件套]
            relic_infos.append(relic_info)
            images.append(Image.open(src + "/" + coordinate_name + "/" + z))
            # print(z)


def generate_relic() -> relic.Relic:
    i = random.randint(0, len(relic_infos) - 1)
    relic0 = relic.Relic(img=images[i]
                         , name=relic_infos[i][2], relic_type=relic_infos[i][1]
                         , coordinate_name=relic_infos[i][0], 两件套=relic_infos[i][3], 四件套=relic_infos[i][4])
    # 随机是否加锁
    relic0.locked = bool(random.randint(0, 1))

    # 随机等级
    relic0.level = random.randint(0, 20)

    # 生成主词条,根据圣遗物类型找对应的主词条列表，再在列表里随机挑选一个词条
    relic0.main_entry_type = relic.Relic.main_entries_n[relic.Relic.relic_types.index(relic0.relic_type)][
        random.randint(0, len(relic.Relic.main_entries_n[relic.Relic.relic_types.index(relic0.relic_type)]) - 1)]

    # 随机生成一个3-4个副词条，并判断如果副词条和主词条相同就重新生成。
    relic0.secondary_lemma_type.clear()
    relic0.secondary_lemma_str.clear()
    z = random.randint(3, 4)
    for i in range(z):
        relic0.secondary_lemma_type.append(
            relic.Relic.secondary_lemmas[random.randint(0, len(relic.Relic.secondary_lemmas) - 1)])

        while relic0.secondary_lemma_type[i] == relic0.main_entry_type \
                or len(set(relic0.secondary_lemma_type)) != len(relic0.secondary_lemma_type):
            relic0.secondary_lemma_type[i] = relic.Relic.secondary_lemmas[
                random.randint(0, len(relic.Relic.secondary_lemmas) - 1)]

    # 生成主词条数值
    if relic0.main_entry_type in relic.Relic.percentage_entries:
        relic0.main_entry_number = random.randint(30, 500) / 10
    else:
        relic0.main_entry_number = random.randint(15, 2000)

    # 生成副词条数值
    for i in range(len(relic0.secondary_lemma_type)):
        if relic0.secondary_lemma_type[i] in relic.Relic.percentage_entries:
            relic0.secondary_lemma_number.append(random.randint(30, 350) / 10)
        else:
            relic0.secondary_lemma_number.append(random.randint(10, 600))

    return relic0


if __name__ == '__main__':
    relic = generate_relic()
    generate_relic_img(relic).show()
    print(relic.tostring())
